package com.example.courseapp;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitHelper {

//    private static final String BASE_URL = "http://192.168.75.146:8080/";
//    private static final String BASE_URL = "https://8572q4p579.vicp.fun/";
//    private static final String BASE_URL = "http://192.168.1.84:9090/";
//    private static final String BASE_URL = "http://172.16.20.180:9090/";
    private static final String BASE_URL = "http://192.168.1.4:9090/";
    private static RetrofitHelper instance;
    private Retrofit retrofit;

    private RetrofitHelper() {
        // 私有构造方法，防止外部实例化
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static synchronized RetrofitHelper getInstance() {
        // 使用双重检查锁定（Double-Checked Locking）确保线程安全
        if (instance == null) {
            synchronized (RetrofitHelper.class) {
                if (instance == null) {
                    instance = new RetrofitHelper();
                }
            }
        }
        return instance;
    }

    public <T> T createService(Class<T> serviceClass) {
        // 创建 Retrofit service 接口实例
        return retrofit.create(serviceClass);
    }
}
